package com.littleHan.brushQuestions.web.specificTopic.controller;

import com.alibaba.fastjson.JSON;
import com.littleHan.brushQuestions.common.Result;
import com.littleHan.brushQuestions.exception.FailedSettingException;
import com.littleHan.brushQuestions.exception.FailedToDeleteException;
import com.littleHan.brushQuestions.exception.FailedToInsertException;
import com.littleHan.brushQuestions.exception.FailedToLoadException;
import com.littleHan.brushQuestions.utils.GetTokenInfoUtil;
import com.littleHan.brushQuestions.web.specificTopic.pojo.vo.TopicPageVo;
import com.littleHan.brushQuestions.web.specificTopic.service.SpecificTopicService;
import com.littleHan.brushQuestions.web.start.pojo.Comment;
import com.littleHan.brushQuestions.web.start.pojo.DicValue;
import com.littleHan.brushQuestions.web.start.pojo.Topic;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@RestController
@RequestMapping("/showTopic")
public class SpecificTopicController {

    @Autowired
    private SpecificTopicService specificTopicService;

    /**
     * 获取当前题目的难易度、题型、科目、内容、发布时间以及上传者及其身份
     *
     * @param topic
     * @return
     * @throws FailedToLoadException
     */
    @RequestMapping(value = "/getTopicInfoJ", method = RequestMethod.POST)
    public String getTopicInfo(@RequestBody Topic topic) throws FailedToLoadException {
        Map<String, Object> map = specificTopicService.getTopicInfo(topic.getId());
        return JSON.toJSONString(new Result().setCode(200).setMessage("获取题目等信息成功").setData(map));
    }

    /**
     * 检查当前题目是否被收藏
     *
     * @param request 请求对象
     * @param topic   含有题目id的对象
     * @return
     * @throws FailedToLoadException
     */
    @RequestMapping(value = "/checkIsCollectTopicJ", method = RequestMethod.POST)
    public String checkIsCollectTopic(HttpServletRequest request, @RequestBody Topic topic) throws FailedToLoadException {
        String userId = GetTokenInfoUtil.getUserId(request);
        boolean isCollect = specificTopicService.checkIsCollectTopic(userId, topic.getId());
        return JSON.toJSONString(new Result().setCode(200).setMessage("获取用户是否收藏该题成功").setData(isCollect));
    }

    /**
     * 收藏于取消收藏
     *
     * @param request 请求对象
     * @param topic   题目对象
     * @return
     * @throws FailedSettingException
     */
    @RequestMapping(value = "/collectAndCancelJ", method = RequestMethod.POST)
    public String collectAndCancel(HttpServletRequest request, @RequestBody Topic topic) throws FailedSettingException {
        String userId = GetTokenInfoUtil.getUserId(request);
        boolean status = specificTopicService.collectAndCancel(userId, topic);
        return JSON.toJSONString(new Result().setCode(200).setMessage("获取用户是否收藏该题成功").setData(status));
    }

    /**
     * 加载评论区
     *
     * @param topicPageVo 携带分页的参数和题目id
     * @param request     请求对象
     * @return
     */
    @PostMapping(value = "/getNewCommentLJ")
    public String getCommentListOrderByTime(@RequestBody TopicPageVo topicPageVo, HttpServletRequest request) {
        //暂时不开发点赞功能，request用于获取当前用户id
        Map<String, Object> map = specificTopicService.getCommentListOrderByTime(topicPageVo, GetTokenInfoUtil.getUserId(request));
        return JSON.toJSONString(new Result().setCode(200).setMessage("获取评论成功").setData(map));
    }

    /**
     * 用户评论
     *
     * @param comment 评论内容、题目id
     * @param request 请求对象
     * @return
     */
    @PostMapping(value = "/userCommentJ")
    public String userComment(@RequestBody Comment comment, HttpServletRequest request) throws FailedToInsertException {
        specificTopicService.userComment(comment, GetTokenInfoUtil.getUserId(request));
        return JSON.toJSONString(new Result().setCode(200).setMessage("评论成功"));
    }

    /**
     * 用户删除自己的评论
     *
     * @param comment 携带评论id
     * @param request 请求对象
     * @return
     * @throws FailedToDeleteException
     */
    @PostMapping(value = "/deleteUserCommentJ")
    public String deleteUserComment(@RequestBody Comment comment, HttpServletRequest request) throws FailedToDeleteException {
        specificTopicService.deleteUserComment(comment.getId(), GetTokenInfoUtil.getUserId(request));
        return JSON.toJSONString(new Result().setCode(200).setMessage("删除评论成功"));
    }

    /**
     * 点赞和取消点赞
     *
     * @param value     true为用户要取消点赞，false为用户要点赞
     * @param commentId 当前评论的id
     * @param request   请求对象
     */
    @GetMapping(value = "/commentLike")
    public String commentLike(boolean value, String commentId, HttpServletRequest request) {
        return JSON.toJSONString(new Result().setCode(200).setMessage("点赞或取消点赞成功").setData(specificTopicService.commentLike(value, commentId, GetTokenInfoUtil.getUserId(request))));
    }
}
